4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
19 using System
.Reflection
;
20 using System
.Reflection
.Emit
;
21 using System
.Globalization
;
22 using System
.Diagnostics
;
24 internal sealed class JSFieldMethod
: JSMethod
{
25 internal FieldInfo field
;
26 internal FunctionObject func
;
28 private static readonly ParameterInfo
[] EmptyParams
= new ParameterInfo
[0];
30 internal JSFieldMethod(FieldInfo field
, Object obj
)
31 : base(obj
){ //The object is never used, but it is convenient to have the field on JSMethod.
36 //If we get here, we know at compile time what function we are calling via the field. I.e. we're in fast mode.
37 Object val
= field
is JSVariableField
? ((JSVariableField
)field
).value : field
.GetValue(null);
38 if (val
is FunctionObject
)
39 this.func
= (FunctionObject
)val
;
42 internal override Object
Construct(Object
[] args
){
43 return LateBinding
.CallValue(this.field
.GetValue(this.obj
), args
, true, false, ((ScriptObject
)this.obj
).engine
, null, JSBinder
.ob
, null, null);
46 public override MethodAttributes Attributes
{
48 if (this.func
!= null)
49 return this.func
.attributes
;
52 return MethodAttributes
.Public
;
53 else if (field
.IsFamily
)
54 return MethodAttributes
.Family
;
55 else if (field
.IsAssembly
)
56 return MethodAttributes
.Assembly
;
58 return MethodAttributes
.Private
;
62 public override Type DeclaringType
{
64 if (this.func
!= null)
65 return Convert
.ToType(this.func
.enclosing_scope
);
71 internal ScriptObject
EnclosingScope(){
72 if (this.func
!= null)
73 return this.func
.enclosing_scope
;
78 public override Object
[] GetCustomAttributes(bool inherit
){
79 if (this.func
!= null){
80 CustomAttributeList caList
= this.func
.customAttributes
;
81 if (caList
!= null) return (Object
[])caList
.Evaluate(inherit
);
86 public override ParameterInfo
[] GetParameters(){
87 if (this.func
!= null)
88 return this.func
.parameter_declarations
;
90 return JSFieldMethod
.EmptyParams
;
93 internal override MethodInfo
GetMethodInfo(CompilerGlobals compilerGlobals
){
94 return this.func
.GetMethodInfo(compilerGlobals
);
98 [DebuggerStepThroughAttribute
]
99 [DebuggerHiddenAttribute
]
101 internal override Object
Invoke(Object obj
, Object thisob
, BindingFlags options
, Binder binder
, Object
[] parameters
, CultureInfo culture
){
102 bool construct
= (options
& BindingFlags
.CreateInstance
) != 0;
103 bool brackets
= (options
& BindingFlags
.GetProperty
) != 0 && (options
& BindingFlags
.InvokeMethod
) == 0;
104 Object f
= this.func
;
105 if (f
== null) f
= this.field
.GetValue(this.obj
);
106 FunctionObject func
= f
as FunctionObject
;
107 JSObject jsOb
= obj
as JSObject
;
108 if (jsOb
!= null && func
!= null && func
.isMethod
&& (func
.attributes
& MethodAttributes
.Virtual
) != 0 &&
109 jsOb
.GetParent() != func
.enclosing_scope
&& ((ClassScope
)func
.enclosing_scope
).HasInstance(jsOb
)){
110 LateBinding lb
= new LateBinding(func
.name
);
112 return lb
.Call(parameters
, construct
, brackets
, ((ScriptObject
)this.obj
).engine
);
114 return LateBinding
.CallValue(f
, parameters
, construct
, brackets
, ((ScriptObject
)this.obj
).engine
, thisob
, binder
, culture
, null);
117 internal bool IsAccessibleFrom(ScriptObject scope
){
118 return ((JSMemberField
)this.field
).IsAccessibleFrom(scope
);
121 public override String Name
{
123 return this.field
.Name
;
127 public override Type ReturnType
{
129 if (this.func
!= null)
130 return Convert
.ToType(this.func
.ReturnType(null));
132 return Typeob
.Object
;
136 internal IReflect
ReturnIR(){
137 if (this.func
!= null)
138 return this.func
.ReturnType(null);
140 return Typeob
.Object
;